<?php
// +----------------------------------------------------------------------
// | FRANSCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013  All rights reserved.
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------

namespace app\user\controller;

use app\common\controller\User;
use think\Db;

/**
 * 推荐
 * Class Promote
 * @package app\user\controller
 */
class Promote extends User
{

    /**
     * @return mixed
     */
    public function index()
    {

        $data = [];
        $data['team_counts'] = Db::name('user')->field('id')->where(['recommend_id' => $this->uid])->find();



        return $this->fetch();
    }

    public function friends()
    {
//        $map = ['mf.uid' => $this->uid, 'm.reg_time' => ['EGT', strtotime('2018-04-14 00:00:00')]];
//
//        $friends_db = Db::name('member_friends');
//        //我的好友 好友统计数据
//        $friends_list = $friends_db->alias('mf')
//            ->field('mf.*, m.reg_time,m.user_phone,(SELECT SUM(ml.investor_capital) FROM lzh_borrow_investor ml WHERE mf.friend_id = ml.investor_uid) investor_capital')
//            ->join('__USER__ m', 'm.id=mf.friend_id', 'LEFT')
//            ->where($map)
//            ->order('mf.rank asc,investor_capital desc')
//            ->select()
//            ->toArray();
//
//
//        //序号	用户	注册时间	好友级别	投资次数	最近投资时间	贡献奖励(元)	推荐好友数	发红包
//        $investor_capital_db = Db::name('borrow_investor');
//        $member_rebate_db = Db::name('member_rebate');
//
//        $total_friend = $total_invest_count = $total_contribution_amount = 0;
//
//        $first_friend = $send_friend = ['total_count' => 0, 'total_contribution_amount' => 0];
//        foreach ($friends_list as $k => $item) {
//            //序号
//            $item['sort'] = $k + 1;
//            //投资次数
//            $invest_info = $investor_capital_db->field('COUNT(id) invest_count, add_time')->where(['investor_uid' => $item['friend_id']])->order('add_time desc')->find();
//            $item['invest_count'] = $invest_info['invest_count'];
//            //最近投资时间
//            $item['last_invest_time'] = $invest_info['add_time'];
//
//            //推荐好友数
//            $item['recommend_count'] = $friends_db->where(['uid' => $item['friend_id'], 'rank' => 1])->count();
//
//            $map = [
//                'target_uid' => $item['friend_id'],
//                'status' => ['IN', [1, 3]],
//                'uid' => $this->uid,
//                'type' => ['IN', [7, 8]]
//            ];
//            //贡献奖励(元)
//            $item['contribution_amount'] = $member_rebate_db->where($map)->sum('reward_amount');
//
//            //是否可以给好友发红包
//            $item['send_card'] = 0;
//            if ($_SERVER['REQUEST_TIME'] - $item['send_card_time'] > 7 * 24 * 3600) $item['send_card'] = 1;
//
//            //累计好友人数
//            $total_friend++;
//            if ($item['rank'] == 1) {
//                $first_friend['total_count']++;
//                $first_friend['total_contribution_amount'] += $item['contribution_amount'];
//            } elseif ($item['rank'] == 2) {
//                $send_friend['total_count']++;
//                $send_friend['total_contribution_amount'] += $item['contribution_amount'];
//            }
//            //投资人数
//            if ($item['investor_capital'] > 0) $total_invest_count++;
//            //累计贡献奖励
//            $total_contribution_amount += $item['contribution_amount'];
//
//            $friends_list[$k] = $item;
//        }
//        $this->assign('first_friend', $first_friend); //好友列表
//        $this->assign('send_friend', $send_friend); //好友列表
//        $this->assign('friend_list', $friends_list); //好友列表
//        $this->assign('total_friend', $total_friend);//累计好友人数
//        $this->assign('total_invest_count', $total_invest_count);  //投资人数
//        $this->assign('total_contribution_amount', $total_contribution_amount);//累计贡献奖励
        return $this->fetch();
    }

//    public function friend()    {return $this->fetch();    }
//    public function reward()    { return $this->fetch();    }

    /**
     * @return mixed
     */
    public function detail()
    {
//        $new_time = strtotime('2018-04-14 00:00:00');
//        $status = input('get.status', 0, 'intval');
//        $member_rebate_db = model('MemberRebate');
//        //奖励时间 add_time	金额（元） reward_amount	奖励类型 type_name	发放状态 status_name	投资次数 1	发放时间 payment_time
//        $map = [
//            'mr.uid' => $this->uid,
//            'mr.status' => $status,
//            'mr.type' => ['IN', [7,8]],
//            'm.reg_time'=>['EGT',$new_time]
//        ];
//
//        if($status == 0) {
//            $map['mr.status'] = ['IN', [1,3]];
//        }
//
//        $rebate_list= $member_rebate_db->getRebateListByPage($map, 'mr.type,mr.reward_amount,mr.status,mr.add_time,mr.payment_time', 'mr.add_time desc');
//
//        foreach($rebate_list as $k => $v){
//            $rebate_list[$k]['payment_time'] = $v['payment_time'] ? date('Y-m-d',$v['payment_time']) : '';
//        }
////        p($member_rebate_db->getLastSql());
////
////        p($rebate_list->toArray());die;
//
//        if($status == 0) {
//            //已发放奖励总额（元）
//            $total_paid = $member_rebate_db->alias('mr')
//                ->join('__MEMBERS__ m','m.id=mr.target_uid')
//                ->where(['m.reg_time'=>['EGT',$new_time],'mr.uid' => $this->uid, 'mr.status' => 1, 'mr.type' => ['IN', [7,8]]])->sum('mr.reward_amount');
//
//            $this->assign('total_paid', $total_paid);
//            //待发佣金（元）
//            $pay_map = [
//                'mr.uid' => $this->uid,
//                'mr.status' => 3,
//                'mr.type' => ['IN', [7,8]],
//                'm.reg_time'=>['EGT',$new_time]
//            ];
//            $total_payment_amount = $member_rebate_db->alias('mr')->join('__MEMBERS__ m','m.id=mr.target_uid')->where($pay_map)->sum('mr.reward_amount');
//            $this->assign('total_payment', $total_payment_amount);// 待发放奖励
//        } else{
//            $total_amount = $member_rebate_db->alias('mr')->join('__MEMBERS__ m','m.id=mr.target_uid')->where($map)->sum('mr.reward_amount');
//            $this->assign('total_amount', $total_amount);
//        }

//        $this->assign('rebate_list', $rebate_list);//奖励详情
        return $this->fetch();
    }
}
